package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.system.domain.Que;
import com.ruoyi.system.domain.Ques;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.web.controller.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题库
 * */
@RestController
@RequestMapping("/api")
public class questionsSubController {
    @Autowired
    private com.ruoyi.system.service.questionsSubService questionsSubService;
    //分页查询
    @GetMapping("/pages/{page}/{pageSize}")
    public R<Page> pageR(@PathVariable("page") int page,@PathVariable("pageSize") int pageSize){
        //分页构造器
        Page<Ques> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Ques> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据id进行排序
        queryWrapper.orderByAsc(Ques::getQuesId);
        //进行分页查询
        questionsSubService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
        //新建题库
    @PostMapping("/saveQues")
    public R<String> save_Ques(@RequestBody Ques ques){
        questionsSubService.save(ques);
        return R.success("新建成功");
    }
    //查看我的题库
    @PostMapping("/myQues")
    public R<List<Ques>> myQues(@RequestBody Ques ques){
        LambdaQueryWrapper<Ques> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Ques::getUserId,ques.getUserId());
        List<Ques> list = questionsSubService.list(queryWrapper);
        return R.success(list);
    }
    @GetMapping("/QuesBy/{quesid}")
    public Results QuesByQuesid(@PathVariable("quesid") Long quesId){
        LambdaQueryWrapper<Ques> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Ques::getQuesId,quesId);
        Ques byId = questionsSubService.getOne(lambdaQueryWrapper);
        return new Results(400,"查询成功",byId);
    }
    //删除题库
    @DeleteMapping
    public R<String> delete_Ques(@RequestParam int id){
        questionsSubService.removeById(id);
        return R.success("删除成功");
    }
    //搜索题库
    @PostMapping("/selectBy/QuesName")
    public R<List<Ques>> selectQues(@RequestBody Ques ques){
        LambdaQueryWrapper<Ques> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Ques::getQuesName,ques.getQuesName());
        List<Ques> list = questionsSubService.list(queryWrapper);
        return R.success(list);
    }
    //根据题库id与题库名查询题库
    @GetMapping("/selectByidandname/{userid}/{quesName}")
    public R<List<Ques>> selectByidAndName(@PathVariable("userid") Integer userid,@PathVariable("quesName") String quesName){
    LambdaQueryWrapper<Ques> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(Ques::getUserId,userid).eq(Ques::getQuesName,quesName);
    List<Ques> list = questionsSubService.list(queryWrapper);
    return R.success(list);
    }

}
